//
//  TextRenderingTools.m
//  TextAnimation
//
//  Created by mac on 2019/4/27.
//  Copyright © 2019 mac. All rights reserved.
//

#import "TextRenderingTools.h"
#import <CoreText/CoreText.h>

static NSInteger i;


@interface TextRenderingTools ()<CAAnimationDelegate>
@property (nonatomic, strong) CALayer *penLayer;
@property (nonatomic, strong) UIImage *penImage;
@property (nonatomic, strong) CAKeyframeAnimation *penAnimation;

@property (nonatomic, assign) BOOL oneMoreText;
@property (nonatomic, strong) NSArray *textArr;



@property (nonatomic, strong) UIColor *bgColor;
@property (nonatomic, strong) UIColor *fontColor;
@property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic, strong) UIFont *font;
@property (nonatomic, strong) UIView *mySuperView;
@property (nonatomic, assign) NSInteger fontSize;
@property (nonatomic, assign) CGFloat fontHeight;

@property (nonatomic, assign) NSInteger lineNumber;

@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;

@end

@implementation TextRenderingTools

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)drawAnimationWithTextArr:(NSArray *)textArr andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
{

    
    CGFloat width = CGRectGetWidth(superView.frame);
    CGFloat height = CGRectGetHeight(superView.frame);
    if (width > 0) {
        _width = width;
    }
    
    if (height > 0) {
        _height = height;
    }
    
    if (fontSize < 1) {
        NSLog(@"fontSize = %lf",_width);
        NSLog(@"请检测fontSize是不是配置有误");
        return;
    }
    
    
    _bgColor = bgColor;
    _fontColor = fontColor;
    _fillColor = fillColor;
    _font = [UIFont systemFontOfSize:fontSize];
    _fontSize = fontSize;
    _mySuperView = superView;
    i = 0;
    _oneMoreText = YES;
    _fontHeight = fontSize + 10;
    _lineNumber = _width / fontSize;
    
    if (_lineNumber < 1) {
        NSLog(@"配置有误");
        return;
    }
    
    NSMutableArray *myTextArr = [NSMutableArray new];
    for (NSString *object in textArr) {
        NSArray *objectArr = [self arrangementText:object withLength:_lineNumber];
        [myTextArr addObjectsFromArray:objectArr];
    }
    
    if (_width < 1) {
        NSLog(@"width = %lf",_width);
        NSLog(@"请检测width是不是配置有误");
    }
    
    if (_height < 1) {
        NSLog(@"height = %lf",_width);
        NSLog(@"请检测height是不是配置有误");
    }
    
    _textArr = [NSArray arrayWithArray:myTextArr];
    if (_textArr.count > i) {
        NSString *text = _textArr[i];
        [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:_fontSize andBounce:CGRectMake(0, i * _fontHeight, _width, _fontHeight) superView:superView];
    }
}

-(void)drawAnimationWithText:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
{
    NSMutableArray *textTempArr = [NSMutableArray new];
    [textTempArr addObject:text];
    
    if (textTempArr.count > 0) {
        [self drawAnimationWithTextArr:textTempArr andFontBgColor:bgColor andFontColor:fontColor andFillColor:fillColor andFontSize:fontSize superView:superView];
    }
}

-(NSArray *)arrangementText:(NSString *)text withLength:(NSInteger)length
{
    NSMutableArray *textTempArr = [NSMutableArray new];
    NSString *textString = text;
    while (textString.length > length) {
        NSString *currentString = [textString substringToIndex:length];
        textString = [textString substringFromIndex:length];
        [textTempArr addObject:currentString];
    }
    if (textString.length > 0) {
        [textTempArr addObject:textString];
    }
    return textTempArr;
}

-(void)drawAnimationWithTextString:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize andBounce:(CGRect)bounce superView:(UIView *)superView
{
    //    _oneMoreText = NO;
    CGFloat multiple = 0;
    NSInteger number = text.length;
    
    
    
    if (number < 5) {
        multiple = 2.0;
    }
    else if (number < 10)
    {
        multiple = 1.9;
    }
    else if (number < 15)
    {
        multiple = 1.8;
    }
    else if (number < 20)
    {
        multiple = 1.5;
    }
    else
    {
        multiple = 1.5;
    }
    CGFloat duration = number * multiple;
    NSLog(@"number = %ld",number);
    NSLog(@"multiple = %lf",multiple);
    NSLog(@"duration = %lf",duration);
    //    CGFloat duration = 10;
    //    CGFloat duration = text.length;
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    UIBezierPath *path = [self getStringLayer:text withFont:font];
    
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = bounce;
    pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
    pathLayer.backgroundColor = [bgColor CGColor];
    pathLayer.geometryFlipped = YES;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = fontColor.CGColor;
    pathLayer.fillColor = fillColor.CGColor;
    pathLayer.lineWidth = 1.0f;
    pathLayer.lineJoin = kCALineJoinMiter;
    [superView.layer addSublayer:pathLayer];
    
    _penImage = [UIImage imageNamed:@"pencil"];
    _penLayer = [CALayer layer];
    _penLayer.contents = (id)_penImage.CGImage;
    _penLayer.anchorPoint = CGPointZero;
    _penLayer.frame = CGRectMake(0.0f, 0.0f, _penImage.size.width, _penImage.size.height);
    [pathLayer addSublayer:_penLayer];
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = duration;
    //    pathAnimation.speed = 5;
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [pathLayer addAnimation:pathAnimation forKey:@"strokeStart"];
    //    pathAnimation.speed = 0.1;
    //    pathAnimation.timeOffset = 0;
    
    _penAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    _penAnimation.duration = duration;
    //    penAnimation.speed = 0.1;
    _penAnimation.removedOnCompletion = YES;
    //    penAnimation.fillMode = kCAFillModeForwards;
    _penAnimation.path = pathLayer.path;
    _penAnimation.calculationMode = kCAAnimationPaced;
    _penAnimation.delegate = self;
    [_penLayer addAnimation:_penAnimation forKey:@"position"];
}

- (UIBezierPath *)getStringLayer:(NSString *)str withFont:(UIFont *)setFont{
    //创建可变path
    CGMutablePathRef letters = CGPathCreateMutable();
    
    //    HelveticaNeue-UltraLight
    //设置字体
    //    CGFontRef myFont = CTFontCreateWithNam, <#CGFloat size#>, <#const CGAffineTransform * _Nullable matrix#>)
    //    CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 32.0f, NULL);
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                           setFont, kCTFontAttributeName,
                           nil];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:str
                                                                     attributes:attrs];
    //根据字符串创建 line
    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
    
    //获取每一个字符作为数组
    CFArrayRef runArray = CTLineGetGlyphRuns(line);
    
    // 遍历字符数组
    for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
    {
        // Get FONT for this run
        CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
        CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
        
        // for each GLYPH in run
        for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
        {
            // get Glyph & Glyph-data
            CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
            CGGlyph glyph;
            CGPoint position;
            CTRunGetGlyphs(run, thisGlyphRange, &glyph);
            CTRunGetPositions(run, thisGlyphRange, &position);
            
            // Get PATH of outline
            {
                CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
                CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
                CGPathAddPath(letters, &t, letter);
                CGPathRelease(letter);
            }
        }
    }
    CFRelease(line);
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointZero];
    [path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];
    CGPathRelease(letters);
    //    CFRelease(setFont);
    return path;
}

-(void)setSuperViewWidth:(CGFloat)superViewWidth
{
    _superViewWidth = superViewWidth;
    _width = superViewWidth;
}

-(void)setSuperViewHeight:(CGFloat)superViewHeight
{
    _superViewHeight = superViewHeight;
    _height = superViewHeight;
}

-(void)setSuperViewSize:(CGSize)superViewSize
{
    _superViewSize = superViewSize;
    self.superViewWidth = superViewSize.width;
    self.superViewHeight = superViewSize.height;
}

- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"开始动画");
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    [_penLayer removeFromSuperlayer];
    //    [_penAnimation removeObserver:self forKeyPath:@"position"];
    
    __weak typeof(self) weakSelf = self;
    if (_oneMoreText) {
        //        static NSInteger i = 1;
        i++;
        if (i < _textArr.count) {
            if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scrollView = (UIScrollView *)_mySuperView;
                scrollView.contentSize = CGSizeMake(_width, (i + 1) * _fontHeight);
                if (scrollView.contentSize.height > _height) {
                    [UIView animateWithDuration:0.5f animations:^{
                        scrollView.contentOffset = CGPointMake(0, (i + 1) * weakSelf.fontHeight - weakSelf.height);
                    } completion:^(BOOL finished) {
                        NSString *text = weakSelf.textArr[i];
                        [weakSelf drawAnimationWithTextString:text andFontBgColor:weakSelf.bgColor andFontColor:weakSelf.fontColor andFillColor:weakSelf.fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * weakSelf.fontHeight, weakSelf.width, weakSelf.fontHeight) superView:weakSelf.mySuperView];
                    }];
                }
                else
                {
                    NSString *text = _textArr[i];
                    [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * _fontHeight, _width, _fontHeight) superView:_mySuperView];
                }
            }
            else
            {
                NSString *text = _textArr[i];
                [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * _fontHeight, _width, _fontHeight) superView:_mySuperView];
            }
        }
        else
        {
            NSLog(@"动画结束");
            
            NSLog(@"销毁工具中的对象---------------1");
            
            _penAnimation = nil;
            
            
            if (self.DrawAnimationFinishedBlock) {
                self.DrawAnimationFinishedBlock();
            }
        }
    }
    else
    {
        NSLog(@"动画结束");
        
        NSLog(@"销毁工具中的对象---------------2");
        
        _penAnimation = nil;
        
        
        if (self.DrawAnimationFinishedBlock) {
            self.DrawAnimationFinishedBlock();
        }
    }
    
    
    
}

-(void)showWithText:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
{
    NSMutableArray *textTempArr = [NSMutableArray new];
    [textTempArr addObject:text];
    
    if (textTempArr.count > 0) {
        [self showTextArr:textTempArr andFontBgColor:bgColor andFontColor:fontColor andFillColor:fillColor andFontSize:fontSize superView:superView];
    }
}

-(void)showTextArr:(NSArray *)textArr andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
{
    
    CGFloat width = CGRectGetWidth(superView.frame);
    CGFloat height = CGRectGetHeight(superView.frame);
    if (width > 0) {
        _width = width;
    }
    
    if (height > 0) {
        _height = height;
    }
    
    if (fontSize < 1) {
        NSLog(@"fontSize = %lf",_width);
        NSLog(@"请检测fontSize是不是配置有误");
        return;
    }
    
    
    _bgColor = bgColor;
    _fontColor = fontColor;
    _fillColor = fillColor;
    _font = [UIFont systemFontOfSize:fontSize];
    _fontSize = fontSize;
    _mySuperView = superView;
    i = 0;
    _oneMoreText = YES;
    _fontHeight = fontSize + 10;
    _lineNumber = _width / fontSize;
    
    if (_lineNumber < 1) {
        NSLog(@"配置有误");
        return;
    }
    
    NSMutableArray *myTextArr = [NSMutableArray new];
    for (NSString *object in textArr) {
        NSArray *objectArr = [self arrangementText:object withLength:_lineNumber];
        [myTextArr addObjectsFromArray:objectArr];
    }
    
    if (_width < 1) {
        NSLog(@"width = %lf",_width);
        NSLog(@"请检测width是不是配置有误");
    }
    
    if (_height < 1) {
        NSLog(@"height = %lf",_width);
        NSLog(@"请检测height是不是配置有误");
    }
    
    
    
    _textArr = [NSArray arrayWithArray:myTextArr];
    
    
    for (NSString *object in _textArr) {
        [self shownimationWithTextString:object andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:_fontSize andBounce:CGRectMake(0, i * _fontHeight, _width, _fontHeight) superView:superView];
        i++;
    }
    if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)_mySuperView;
        scrollView.contentSize = CGSizeMake(_width, (i + 0) * _fontHeight);
    }
    
}

-(void)shownimationWithTextString:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize andBounce:(CGRect)bounce superView:(UIView *)superView
{
    
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    UIBezierPath *path = [self getStringLayer:text withFont:font];
    
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.frame = bounce;
    pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
    pathLayer.backgroundColor = [bgColor CGColor];
    pathLayer.geometryFlipped = YES;
    pathLayer.path = path.CGPath;
    pathLayer.strokeColor = fontColor.CGColor;
    pathLayer.fillColor = fillColor.CGColor;
    pathLayer.lineWidth = 1.0f;
    pathLayer.lineJoin = kCALineJoinMiter;
    [superView.layer addSublayer:pathLayer];
}

-(void)dealloc
{
    NSLog(@"销毁文字动画工具控制器");
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
































////
////  TextRenderingTools.m
////  TextAnimation
////
////  Created by mac on 2019/4/27.
////  Copyright © 2019 mac. All rights reserved.
////
//
//#import "TextRenderingTools.h"
//#import <CoreText/CoreText.h>
//
//static NSInteger i;
//
//
//@interface TextRenderingTools ()<CAAnimationDelegate>
//@property (nonatomic, strong) CALayer *penLayer;
//@property (nonatomic, strong) UIImage *penImage;
//@property (nonatomic, strong) CAKeyframeAnimation *penAnimation;
//
//@property (nonatomic, assign) BOOL oneMoreText;
//@property (nonatomic, strong) NSArray *textArr;
//
//
//
//@property (nonatomic, strong) UIColor *bgColor;
//@property (nonatomic, strong) UIColor *fontColor;
//@property (nonatomic, strong) UIColor *fillColor;
//@property (nonatomic, strong) UIFont *font;
//@property (nonatomic, strong) UIView *mySuperView;
//@property (nonatomic, assign) NSInteger fontSize;
//@property (nonatomic, assign) CGFloat fontHeight;
//
//@property (nonatomic, assign) NSInteger lineNumber;
//
//@property (nonatomic, assign) CGFloat width;
//
//@end
//
//@implementation TextRenderingTools
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do any additional setup after loading the view.
//}
//
//-(void)drawAnimationWithTextArr:(NSArray *)textArr andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
//{
//    _width = CGRectGetWidth(superView.frame);
//
//
//    _bgColor = bgColor;
//    _fontColor = fontColor;
//    _fillColor = fillColor;
//    _font = [UIFont systemFontOfSize:fontSize];
//    _fontSize = fontSize;
//    _mySuperView = superView;
//    i = 0;
//    _oneMoreText = YES;
//    _fontHeight = fontSize + 10;
//    _lineNumber = kScreenWidth / fontSize;
//
//    NSMutableArray *myTextArr = [NSMutableArray new];
//    for (NSString *object in textArr) {
//        NSArray *objectArr = [self arrangementText:object withLength:_lineNumber];
//        [myTextArr addObjectsFromArray:objectArr];
//    }
//
//
//    _textArr = [NSArray arrayWithArray:myTextArr];
//    if (_textArr.count > i) {
//        NSString *text = _textArr[i];
//        [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:_fontSize andBounce:CGRectMake(0, i * _fontHeight, kScreenWidth, _fontHeight) superView:superView];
//    }
//}
//
//-(void)drawAnimationWithText:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
//{
//    NSMutableArray *textTempArr = [NSMutableArray new];
//    [textTempArr addObject:text];
//
//    if (textTempArr.count > 0) {
//        [self drawAnimationWithTextArr:textTempArr andFontBgColor:bgColor andFontColor:fontColor andFillColor:fillColor andFontSize:fontSize superView:superView];
//    }
//}
//
//-(NSArray *)arrangementText:(NSString *)text withLength:(NSInteger)length
//{
//    NSMutableArray *textTempArr = [NSMutableArray new];
//    NSString *textString = text;
//    while (textString.length > length) {
//        NSString *currentString = [textString substringToIndex:length];
//        textString = [textString substringFromIndex:length];
//        [textTempArr addObject:currentString];
//    }
//    if (textString.length > 0) {
//        [textTempArr addObject:textString];
//    }
//    return textTempArr;
//}
//
//-(void)drawAnimationWithTextString:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize andBounce:(CGRect)bounce superView:(UIView *)superView
//{
//    //    _oneMoreText = NO;
//    CGFloat multiple = 0;
//    NSInteger number = text.length;
//
//
//
//    if (number < 5) {
//        multiple = 2.0;
//    }
//    else if (number < 10)
//    {
//        multiple = 1.9;
//    }
//    else if (number < 15)
//    {
//        multiple = 1.8;
//    }
//    else if (number < 20)
//    {
//        multiple = 1.5;
//    }
//    else
//    {
//        multiple = 1.5;
//    }
//    CGFloat duration = number * multiple;
//    NSLog(@"number = %ld",number);
//    NSLog(@"multiple = %lf",multiple);
//    NSLog(@"duration = %lf",duration);
//    //    CGFloat duration = 10;
//    //    CGFloat duration = text.length;
//    UIFont *font = [UIFont systemFontOfSize:fontSize];
//    UIBezierPath *path = [self getStringLayer:text withFont:font];
//
//    CAShapeLayer *pathLayer = [CAShapeLayer layer];
//    pathLayer.frame = bounce;
//    pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
//    //    pathLayer.bounds = CGRectMake(20, 100, kScreenWidth, 60);
//    pathLayer.backgroundColor = [bgColor CGColor];
//    pathLayer.geometryFlipped = YES;
//    pathLayer.path = path.CGPath;
//    pathLayer.strokeColor = fontColor.CGColor;
//    pathLayer.fillColor = fillColor.CGColor;
//    pathLayer.lineWidth = 1.0f;
//    pathLayer.lineJoin = kCALineJoinMiter;
//    [superView.layer addSublayer:pathLayer];
//
//    _penImage = [UIImage imageNamed:@"pencil"];
//    _penLayer = [CALayer layer];
//    _penLayer.contents = (id)_penImage.CGImage;
//    _penLayer.anchorPoint = CGPointZero;
//    _penLayer.frame = CGRectMake(0.0f, 0.0f, _penImage.size.width, _penImage.size.height);
//    [pathLayer addSublayer:_penLayer];
//
//    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
//    pathAnimation.duration = duration;
//    //    pathAnimation.speed = 5;
//    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
//    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
//    [pathLayer addAnimation:pathAnimation forKey:@"strokeStart"];
//    //    pathAnimation.speed = 0.1;
//    //    pathAnimation.timeOffset = 0;
//
//    _penAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//    _penAnimation.duration = duration;
//    //    penAnimation.speed = 0.1;
//    _penAnimation.removedOnCompletion = YES;
//    //    penAnimation.fillMode = kCAFillModeForwards;
//    _penAnimation.path = pathLayer.path;
//    _penAnimation.calculationMode = kCAAnimationPaced;
//    _penAnimation.delegate = self;
//    [_penLayer addAnimation:_penAnimation forKey:@"position"];
//}
//
//- (UIBezierPath *)getStringLayer:(NSString *)str withFont:(UIFont *)setFont{
//    //创建可变path
//    CGMutablePathRef letters = CGPathCreateMutable();
//
//    //    HelveticaNeue-UltraLight
//    //设置字体
//    //    CGFontRef myFont = CTFontCreateWithNam, <#CGFloat size#>, <#const CGAffineTransform * _Nullable matrix#>)
//    //    CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 32.0f, NULL);
//    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
//                           setFont, kCTFontAttributeName,
//                           nil];
//    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:str
//                                                                     attributes:attrs];
//    //根据字符串创建 line
//    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
//
//    //获取每一个字符作为数组
//    CFArrayRef runArray = CTLineGetGlyphRuns(line);
//
//    // 遍历字符数组
//    for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
//    {
//        // Get FONT for this run
//        CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
//        CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
//
//        // for each GLYPH in run
//        for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
//        {
//            // get Glyph & Glyph-data
//            CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
//            CGGlyph glyph;
//            CGPoint position;
//            CTRunGetGlyphs(run, thisGlyphRange, &glyph);
//            CTRunGetPositions(run, thisGlyphRange, &position);
//
//            // Get PATH of outline
//            {
//                CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
//                CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
//                CGPathAddPath(letters, &t, letter);
//                CGPathRelease(letter);
//            }
//        }
//    }
//    CFRelease(line);
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointZero];
//    [path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];
//    CGPathRelease(letters);
//    //    CFRelease(setFont);
//    return path;
//}
//
//- (void)animationDidStart:(CAAnimation *)anim
//{
//    NSLog(@"开始动画");
//}
//
//-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
//{
//    [_penLayer removeFromSuperlayer];
//    //    [_penAnimation removeObserver:self forKeyPath:@"position"];
//
//    __weak typeof(self) weakSelf = self;
//    if (_oneMoreText) {
//        //        static NSInteger i = 1;
//        i++;
//        if (i < _textArr.count) {
//            if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
//                UIScrollView *scrollView = (UIScrollView *)_mySuperView;
//                scrollView.contentSize = CGSizeMake(kScreenWidth, (i + 1) * _fontHeight);
//                if (scrollView.contentSize.height > kScreenHeight) {
//                    [UIView animateWithDuration:0.5f animations:^{
//                        scrollView.contentOffset = CGPointMake(0, (i + 1) * weakSelf.fontHeight - kScreenHeight);
//                    } completion:^(BOOL finished) {
//                        NSString *text = weakSelf.textArr[i];
//                        [weakSelf drawAnimationWithTextString:text andFontBgColor:weakSelf.bgColor andFontColor:weakSelf.fontColor andFillColor:weakSelf.fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * weakSelf.fontHeight, kScreenWidth, weakSelf.fontHeight) superView:weakSelf.mySuperView];
//                    }];
//                }
//                else
//                {
//                    NSString *text = _textArr[i];
//                    [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * _fontHeight, kScreenWidth, _fontHeight) superView:_mySuperView];
//                }
//            }
//            else
//            {
//                NSString *text = _textArr[i];
//                [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * _fontHeight, kScreenWidth, _fontHeight) superView:_mySuperView];
//            }
//        }
//        else
//        {
//            NSLog(@"动画结束");
//
//            NSLog(@"销毁工具中的对象---------------1");
//
//            _penAnimation = nil;
//
//
//            if (self.DrawAnimationFinishedBlock) {
//                self.DrawAnimationFinishedBlock();
//            }
//        }
//    }
//    else
//    {
//        NSLog(@"动画结束");
//
//        NSLog(@"销毁工具中的对象---------------2");
//
//        _penAnimation = nil;
//
//
//        if (self.DrawAnimationFinishedBlock) {
//            self.DrawAnimationFinishedBlock();
//        }
//    }
//
//
//
//}
//
//-(void)showWithText:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
//{
//    NSMutableArray *textTempArr = [NSMutableArray new];
//    [textTempArr addObject:text];
//
//    if (textTempArr.count > 0) {
//        [self showTextArr:textTempArr andFontBgColor:bgColor andFontColor:fontColor andFillColor:fillColor andFontSize:fontSize superView:superView];
//    }
//}
//
//-(void)showTextArr:(NSArray *)textArr andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize superView:(UIView *)superView
//{
//    _bgColor = bgColor;
//    _fontColor = fontColor;
//    _fillColor = fillColor;
//    _font = [UIFont systemFontOfSize:fontSize];
//    _fontSize = fontSize;
//    _mySuperView = superView;
//    i = 0;
//    _oneMoreText = YES;
//    _fontHeight = fontSize + 10;
//    _lineNumber = kScreenWidth / fontSize;
//
//    NSMutableArray *myTextArr = [NSMutableArray new];
//    for (NSString *object in textArr) {
//        NSArray *objectArr = [self arrangementText:object withLength:_lineNumber];
//        [myTextArr addObjectsFromArray:objectArr];
//    }
//
//
//    _textArr = [NSArray arrayWithArray:myTextArr];
//
//
//    for (NSString *object in _textArr) {
//        [self shownimationWithTextString:object andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:_fontSize andBounce:CGRectMake(0, i * _fontHeight, kScreenWidth, _fontHeight) superView:superView];
//        i++;
//    }
//    UIScrollView *scrollView = (UIScrollView *)_mySuperView;
//    scrollView.contentSize = CGSizeMake(kScreenWidth, (i + 0) * _fontHeight);
//}
//
//-(void)shownimationWithTextString:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize andBounce:(CGRect)bounce superView:(UIView *)superView
//{
//
//    UIFont *font = [UIFont systemFontOfSize:fontSize];
//    UIBezierPath *path = [self getStringLayer:text withFont:font];
//
//    CAShapeLayer *pathLayer = [CAShapeLayer layer];
//    pathLayer.frame = bounce;
//    pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
//    pathLayer.backgroundColor = [bgColor CGColor];
//    pathLayer.geometryFlipped = YES;
//    pathLayer.path = path.CGPath;
//    pathLayer.strokeColor = fontColor.CGColor;
//    pathLayer.fillColor = fillColor.CGColor;
//    pathLayer.lineWidth = 1.0f;
//    pathLayer.lineJoin = kCALineJoinMiter;
//    [superView.layer addSublayer:pathLayer];
//}
//
//-(void)dealloc
//{
//    NSLog(@"销毁文字动画工具控制器");
//}
//
///*
// #pragma mark - Navigation
//
// // In a storyboard-based application, you will often want to do a little preparation before navigation
// - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// // Get the new view controller using [segue destinationViewController].
// // Pass the selected object to the new view controller.
// }
// */
//
//@end



































////
////  TextRenderingTools.m
////  TextAnimation
////
////  Created by mac on 2019/4/27.
////  Copyright © 2019 mac. All rights reserved.
////
//
//#import "TextRenderingTools.h"
//#import <CoreText/CoreText.h>
//
//static NSInteger i;
//
//
//@interface TextRenderingTools ()<CAAnimationDelegate>
//@property (nonatomic, strong) CALayer *penLayer;
//@property (nonatomic, strong) UIImage *penImage;
//@property (nonatomic, strong) CAKeyframeAnimation *penAnimation;
//
//@property (nonatomic, assign) BOOL oneMoreText;
//@property (nonatomic, strong) NSArray *textArr;
//
//
//
//@property (nonatomic, strong) UIColor *bgColor;
//@property (nonatomic, strong) UIColor *fontColor;
//@property (nonatomic, strong) UIColor *fillColor;
//@property (nonatomic, strong) UIFont *font;
//@property (nonatomic, strong) UIView *mySuperView;
//@property (nonatomic, assign) NSInteger fontSize;
//@property (nonatomic, assign) CGFloat fontHeight;
//
//@property (nonatomic, assign) NSInteger lineNumber;
//
//@end
//
//@implementation TextRenderingTools
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do any additional setup after loading the view.
//}
//
//-(void)drawAnimationWithTextArr:(NSArray *)textArr andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize andBounce:(CGRect)bounce superView:(UIView *)superView
//{
//    _bgColor = bgColor;
//    _fontColor = fontColor;
//    _fillColor = fillColor;
//    _font = [UIFont systemFontOfSize:fontSize];
//    _fontSize = fontSize;
//    _mySuperView = superView;
//    i = 0;
//    _oneMoreText = YES;
//    _fontHeight = fontSize + 10;
//    _lineNumber = kScreenWidth / fontSize;
//
//    NSMutableArray *myTextArr = [NSMutableArray new];
//    for (NSString *object in textArr) {
//        NSArray *objectArr = [self arrangementText:object withLength:_lineNumber];
//        [myTextArr addObjectsFromArray:objectArr];
//    }
//
//
//    _textArr = [NSArray arrayWithArray:myTextArr];
//    if (_textArr.count > i) {
//        NSString *text = _textArr[i];
//        [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:_fontSize andBounce:CGRectMake(0, i * _fontHeight, kScreenWidth, _fontHeight) superView:superView];
//    }
//}
//
//-(void)drawAnimationWithText:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize andBounce:(CGRect)bounce superView:(UIView *)superView
//{
//    NSMutableArray *textTempArr = [NSMutableArray new];
//    [textTempArr addObject:text];
//
//    if (textTempArr.count > 0) {
//        [self drawAnimationWithTextArr:textTempArr andFontBgColor:bgColor andFontColor:fontColor andFillColor:fillColor andFontSize:fontSize andBounce:bounce superView:superView];
//    }
//}
//
//-(NSArray *)arrangementText:(NSString *)text withLength:(NSInteger)length
//{
//    NSMutableArray *textTempArr = [NSMutableArray new];
//    NSString *textString = text;
//    while (textString.length > length) {
//        NSString *currentString = [textString substringToIndex:length];
//        textString = [textString substringFromIndex:length];
//        [textTempArr addObject:currentString];
//    }
//    if (textString.length > 0) {
//        [textTempArr addObject:textString];
//    }
//    return textTempArr;
//}
//
//-(void)drawAnimationWithTextString:(NSString *)text andFontBgColor:(UIColor *)bgColor andFontColor:(UIColor *)fontColor andFillColor:(UIColor *)fillColor andFontSize:(NSInteger)fontSize andBounce:(CGRect)bounce superView:(UIView *)superView
//{
////    _oneMoreText = NO;
//    CGFloat multiple = 0;
//    NSInteger number = text.length;
//
//    if (number < 5) {
//        multiple = 2.0;
//    }
//    else if (number < 10)
//    {
//        multiple = 1.9;
//    }
//    else if (number < 15)
//    {
//        multiple = 1.8;
//    }
//    else if (number < 20)
//    {
//        multiple = 1.5;
//    }
//    else
//    {
//        multiple = 1.5;
//    }
//    CGFloat duration = number * multiple;
//    NSLog(@"number = %ld",number);
//    NSLog(@"multiple = %lf",multiple);
//    NSLog(@"duration = %lf",duration);
////    CGFloat duration = 10;
////    CGFloat duration = text.length;
//    UIFont *font = [UIFont systemFontOfSize:fontSize];
//    UIBezierPath *path = [self getStringLayer:text withFont:font];
//
//    CAShapeLayer *pathLayer = [CAShapeLayer layer];
//    pathLayer.frame = bounce;
//    pathLayer.bounds = CGPathGetBoundingBox(path.CGPath);
//    //    pathLayer.bounds = CGRectMake(20, 100, kScreenWidth, 60);
//    pathLayer.backgroundColor = [bgColor CGColor];
//    pathLayer.geometryFlipped = YES;
//    pathLayer.path = path.CGPath;
//    pathLayer.strokeColor = fontColor.CGColor;
//    pathLayer.fillColor = fillColor.CGColor;
//    pathLayer.lineWidth = 1.0f;
//    pathLayer.lineJoin = kCALineJoinMiter;
//    [superView.layer addSublayer:pathLayer];
//
//    _penImage = [UIImage imageNamed:@"pencil"];
//    _penLayer = [CALayer layer];
//    _penLayer.contents = (id)_penImage.CGImage;
//    _penLayer.anchorPoint = CGPointZero;
//    _penLayer.frame = CGRectMake(0.0f, 0.0f, _penImage.size.width, _penImage.size.height);
//    [pathLayer addSublayer:_penLayer];
//
//    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
//    pathAnimation.duration = duration;
////    pathAnimation.speed = 5;
//    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
//    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
//    [pathLayer addAnimation:pathAnimation forKey:@"strokeStart"];
////    pathAnimation.speed = 0.1;
////    pathAnimation.timeOffset = 0;
//
//    _penAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//    _penAnimation.duration = duration;
////    penAnimation.speed = 0.1;
//    _penAnimation.removedOnCompletion = YES;
////    penAnimation.fillMode = kCAFillModeForwards;
//    _penAnimation.path = pathLayer.path;
//    _penAnimation.calculationMode = kCAAnimationPaced;
//    _penAnimation.delegate = self;
//    [_penLayer addAnimation:_penAnimation forKey:@"position"];
//}
//
//- (UIBezierPath *)getStringLayer:(NSString *)str withFont:(UIFont *)setFont{
//    //创建可变path
//    CGMutablePathRef letters = CGPathCreateMutable();
//
//    //    HelveticaNeue-UltraLight
//    //设置字体
//    //    CGFontRef myFont = CTFontCreateWithNam, <#CGFloat size#>, <#const CGAffineTransform * _Nullable matrix#>)
//    //    CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 32.0f, NULL);
//    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
//                           setFont, kCTFontAttributeName,
//                           nil];
//    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:str
//                                                                     attributes:attrs];
//    //根据字符串创建 line
//    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attrString);
//
//    //获取每一个字符作为数组
//    CFArrayRef runArray = CTLineGetGlyphRuns(line);
//
//    // 遍历字符数组
//    for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runArray); runIndex++)
//    {
//        // Get FONT for this run
//        CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, runIndex);
//        CTFontRef runFont = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
//
//        // for each GLYPH in run
//        for (CFIndex runGlyphIndex = 0; runGlyphIndex < CTRunGetGlyphCount(run); runGlyphIndex++)
//        {
//            // get Glyph & Glyph-data
//            CFRange thisGlyphRange = CFRangeMake(runGlyphIndex, 1);
//            CGGlyph glyph;
//            CGPoint position;
//            CTRunGetGlyphs(run, thisGlyphRange, &glyph);
//            CTRunGetPositions(run, thisGlyphRange, &position);
//
//            // Get PATH of outline
//            {
//                CGPathRef letter = CTFontCreatePathForGlyph(runFont, glyph, NULL);
//                CGAffineTransform t = CGAffineTransformMakeTranslation(position.x, position.y);
//                CGPathAddPath(letters, &t, letter);
//                CGPathRelease(letter);
//            }
//        }
//    }
//    CFRelease(line);
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointZero];
//    [path appendPath:[UIBezierPath bezierPathWithCGPath:letters]];
//    CGPathRelease(letters);
//    //    CFRelease(setFont);
//    return path;
//}
//
//- (void)animationDidStart:(CAAnimation *)anim
//{
//    NSLog(@"开始动画");
//}
//
//-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
//{
//    [_penLayer removeFromSuperlayer];
////    [_penAnimation removeObserver:self forKeyPath:@"position"];
//
//    __weak typeof(self) weakSelf = self;
//    if (_oneMoreText) {
////        static NSInteger i = 1;
//        i++;
//        if (i < _textArr.count) {
//            if ([_mySuperView isKindOfClass:[UIScrollView class]]) {
//                UIScrollView *scrollView = (UIScrollView *)_mySuperView;
//                scrollView.contentSize = CGSizeMake(kScreenWidth, (i + 1) * _fontHeight);
//                if (scrollView.contentSize.height > kScreenHeight) {
//                    [UIView animateWithDuration:0.5f animations:^{
//                        scrollView.contentOffset = CGPointMake(0, (i + 1) * weakSelf.fontHeight - kScreenHeight);
//                    } completion:^(BOOL finished) {
//                        NSString *text = weakSelf.textArr[i];
//                        [weakSelf drawAnimationWithTextString:text andFontBgColor:weakSelf.bgColor andFontColor:weakSelf.fontColor andFillColor:weakSelf.fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * weakSelf.fontHeight, kScreenWidth, weakSelf.fontHeight) superView:weakSelf.mySuperView];
//                    }];
//                }
//                else
//                {
//                    NSString *text = _textArr[i];
//                    [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * _fontHeight, kScreenWidth, _fontHeight) superView:_mySuperView];
//                }
//            }
//            else
//            {
//                NSString *text = _textArr[i];
//                [self drawAnimationWithTextString:text andFontBgColor:_bgColor andFontColor:_fontColor andFillColor:_fillColor andFontSize:weakSelf.fontSize andBounce:CGRectMake(0, i * _fontHeight, kScreenWidth, _fontHeight) superView:_mySuperView];
//            }
//        }
//        else
//        {
//            NSLog(@"动画结束");
//
//            NSLog(@"销毁工具中的对象---------------1");
//
//            _penAnimation = nil;
//
//
//            if (self.DrawAnimationFinishedBlock) {
//                self.DrawAnimationFinishedBlock();
//            }
//        }
//    }
//    else
//    {
//        NSLog(@"动画结束");
//
//        NSLog(@"销毁工具中的对象---------------2");
//
//        _penAnimation = nil;
//
//
//        if (self.DrawAnimationFinishedBlock) {
//            self.DrawAnimationFinishedBlock();
//        }
//    }
//
//
//
//}
//
//-(void)dealloc
//{
//    NSLog(@"销毁文字动画工具控制器");
//}
//
///*
//#pragma mark - Navigation
//
//// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    // Get the new view controller using [segue destinationViewController].
//    // Pass the selected object to the new view controller.
//}
//*/
//
//@end
